/******************************************************************************
*
*  MP3Play Widget -- PlayStrip
*  Created by Patrick Neeley <patrick@neeley.org>
*             http://www.neeley.org/
*  Created on 2007.08.02
*  Updated on 2007.08.03
*
*  Description:

   A component that contains a flash SoundPlayer object, and provides a 
   bare-bones user interface for it.

******************************************************************************/

import flash.MovieClip;
import flash.Sound;


class PlayStrip extends Component
{
  var g_root : MovieClip;

  var g_bAllowDownload : Bool;

    // sound player controls
  var g_SoundPlayer : Sound;

    // progress controls
  var g_ProgressBar : Component;
  var g_TimeTrack : Component;
  var g_Clock : Component;
  static var g_clockWidth : Float = 42;
  static var g_trackWidth : Float = 0;
  var g_Timer : haxe.Timer;
  var g_songpaused : Bool;
  var g_playing : Bool;

  static var g_TrackColor : Int = 0xFFFFFF;
  static var g_TrackText : Int = 0x000000;
  static var g_TrackHilite : Int = 0xAFAFEF;
  static var g_TrackHiliteText : Int = 0x000000;

    // currently selected song file
  var g_songnum : Int;
  var g_songfile : String;
  var g_songtitle : String;
  var g_songdlfile : String;

    // play buttons
  var g_btnPlay : Button;
  var g_btnStop : Button;
  var g_btnDownload : Button;


  public function new (root, bAllowDownload:Bool, x, y, w, h, ?color, ?text, ?trackcolor, ?tracktext, ?trackhilite, ?trackhilitetext)
  {
    super(root, false);
    g_root = root;
    this.move(x, y, w, h);

    (color == null) ? this.setColor(0x000000): this.setColor(color);
    (text == null) ? this.setTextColor(0xFFFFFF): this.setTextColor(text);
    if (trackcolor != null) { g_TrackColor = trackcolor; }
    if (tracktext != null) { g_TrackText = tracktext; }
    if (trackhilite != null) { g_TrackHilite = trackhilite; }
    if (trackhilitetext != null) { g_TrackHiliteText = trackhilitetext; }

    var ntrackheight = (h/2);

    g_bAllowDownload = bAllowDownload;
    g_playing = false;

      // create the time track
    g_ProgressBar = new Component(this.mc, true);
    g_ProgressBar.move(0, 0, w, ntrackheight);
    g_ProgressBar.setAlpha(100);
    g_ProgressBar.setColor(g_TrackColor);
    g_ProgressBar.setTextColor(g_TrackText);

      // create the time track progress indicator
    g_TimeTrack = new Component(this.mc, true);
    g_TimeTrack.move(0, 0, 0, ntrackheight);
    g_TimeTrack.setAlpha(100);
    g_TimeTrack.setColor(g_TrackHilite);
    g_TimeTrack.setTextColor(g_TrackHiliteText);
    g_trackWidth = w - g_clockWidth;

      // create the clock
    g_Clock = new Component(this.mc, true);
    g_Clock.move(w - g_clockWidth, 0, g_clockWidth, ntrackheight);
    g_Clock.setColor(g_TrackColor);
    g_Clock.setTextColor(g_TrackText);

      // update fonts for displays
    var f = g_ProgressBar.getNewTextFormat();
    f.size = 11;
    f.bold = true;
    f.color = g_TrackText;
    g_ProgressBar.setNewTextFormat(f);
    f.color = g_TrackHiliteText;
    g_TimeTrack.setNewTextFormat(f);
    f.align="center";
    g_Clock.setNewTextFormat(f);

      // create play buttons
    var y = ntrackheight + (ntrackheight / 2) - 5;
    g_btnPlay = new Button(this.mc, "playimg", false, 0, y, 10, 10);
    g_btnStop = new Button(this.mc, "stopimg", false, 0, y, 10, 10);
    var me = this;
    g_btnPlay.onPress = function () { me.PlayFromStart(); }
    g_btnStop.onPress = function () { me.Stop(); }
    g_btnPlay.setVisible(false);
    g_btnStop.setVisible(false);

    g_btnPlay.setAlpha(0);

    if (g_bAllowDownload) {
      g_btnDownload = new Button(this.mc, "DOWNLOAD", true, w - 80, y - 3, 80, 16);
      f = g_btnDownload.getTextFormat();
      f.size = 10;
      f.bold = true;
      f.align="right";
      g_btnDownload.setTextFormat(f);
      g_btnDownload.setColor(color);
      g_btnDownload.setTextColor(text);
      g_btnDownload.setVisible(false);
      g_btnDownload.onPress = DownloadHandler;
    }
  }
  // end new


  public function setSongFile (num:Int, file:String, title:String, dlfile:String)
  {
    g_songnum = num;
    g_songfile = file;
    g_songtitle = title;
    g_songdlfile = dlfile;
    g_ProgressBar.setText(title);
    if (g_bAllowDownload) {
      g_btnDownload.setVisible(true);
    }
    this.PlayFromStart();
  }
  // end setSongFile


  private function onSoundCompleteHandler ()
  {
    g_SoundPlayer.stop();
    g_TimeTrack.setWidth(g_trackWidth);
    g_Timer.stop();
    g_btnStop.setVisible(false);
    g_btnPlay.setVisible(true);
    this.onSoundCompleteNotify(g_songnum, g_songfile, g_songtitle);
  }
  // end onSoundCompleteHandler

  public function onSoundCompleteNotify (num:Int, file:String, title:String)
  {
    // placeholder-- implement this to do something when a song is done playing
  }


  function Stop ()
  {
    this.resetClock();
    g_SoundPlayer.stop();
    g_SoundPlayer.loadSound(null, false);
    g_btnStop.setVisible(false);
    g_btnPlay.setVisible(true);
    g_playing = false;
  }
  // end Stop


  function PlayFromStart ()
  {
    this.resetClock();
    g_SoundPlayer.stop();
    g_SoundPlayer.loadSound(null, false);
    g_btnPlay.setVisible(false);
    g_btnStop.setVisible(false);
    g_playing = false;

    if (g_songfile != "") {
        // create and initialize the sound player
      g_SoundPlayer = new flash.Sound(g_root);
      g_SoundPlayer.onSoundComplete = onSoundCompleteHandler;
      g_SoundPlayer.loadSound(g_songfile, true);
      g_SoundPlayer.start();
      this.startClock();
      g_btnStop.setVisible(true);
      g_playing = true;
    }
  }
  // end PlayFromStart


  private function updateClock ()
  {
    var p = g_SoundPlayer.position;
    var d = g_SoundPlayer.duration;

    var s = Math.floor(p / 1000);
    var m = Math.floor(s / 60);
    s -= (m * 60);

    var ct : String = 
      ((m < 10) ? "0": "") + m + ":" +
      ((s < 10) ? "0": "") + s;
    g_Clock.setText(ct);

    var w = Math.ceil( ((p / (d / 100)) / 100) * g_trackWidth );
    g_TimeTrack.setWidth(w);
  }
  // end updateClock


  private function startClock ()
  {
    g_Timer.stop();
    g_TimeTrack.setWidth(0);
    g_TimeTrack.setText(g_ProgressBar.getText());
    g_Clock.setColor(g_TrackHilite);
    g_Clock.setText("00:00");
    g_Timer = new haxe.Timer(500);
    g_Timer.run = updateClock;
  }
  // end startClock


  private function stopClock ()
  {
    g_Timer.stop();
  }
  // end stopClock


  private function resetClock ()
  {
    g_Timer.stop();
    g_Clock.setColor(g_TrackColor);
    g_Clock.setText("");
    g_TimeTrack.setWidth(0);
  }
  // end resetClock


    // this function is also in SongList.hx
  private function DownloadHandler ()
  {
    if (g_bAllowDownload) {
      onDownload(g_songdlfile);
    }
  }
  public function onDownload (dlfile:String) {
    // This is just a placeholder; it must be implemented after an instance
    // of this class has been created.
  }


  public function setColors (?color, ?text, ?trackcolor, ?tracktext, ?trackhilite, ?trackhilitetext)
  {
    (color == null) ? this.setColor(0x000000): this.setColor(color);
    (text == null) ? this.setTextColor(0xFFFFFF): this.setTextColor(text);
    if (trackcolor != null) { g_TrackColor = trackcolor; }
    if (tracktext != null) { g_TrackText = tracktext; }
    if (trackhilite == null) { g_TrackHilite = trackhilite; }
    if (trackhilitetext == null) { g_TrackHilite = trackhilitetext; }

    g_ProgressBar.setColor(g_TrackColor);
    g_ProgressBar.setTextColor(g_TrackText);
    g_TimeTrack.setColor(g_TrackHilite);
    g_TimeTrack.setTextColor(g_TrackHiliteText);
    g_Clock.setColor(g_playing ? g_TrackHilite: g_TrackColor);
    g_Clock.setTextColor(g_playing ? g_TrackHiliteText: g_TrackText);
    if (g_btnDownload != null) {
      g_btnDownload.setColor(color);
      g_btnDownload.setTextColor(text);
    }
  }
  // end setColors


  public function destroy ()
  {
    if (g_Clock != null) {
      g_Clock.destroy();
    }
    if (g_TimeTrack != null) {
      g_TimeTrack.destroy();
    }
    if (g_ProgressBar != null) {
      g_ProgressBar.destroy();
    }
    if (g_btnPlay != null) {
      g_btnPlay.destroy();
    }
    if (g_btnStop != null) {
      g_btnStop.destroy();
    }
    if (g_btnDownload != null) {
      g_btnDownload.destroy();
    }
    super.destroy();
  }
  // end destroy

}
// end class PlayStrip
